package nowCoder;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class HJ45 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            String x = in.next();
            int[][] xx = new int[26][2];
            for (int j = 0; j < 26; j++) {
                xx[j][0] = j;
                xx[j][1] = 0;
            }
            for (int j = 0; j < x.length(); j++) {
                int m = x.charAt(j);
                xx[m-97][1]++;
            }
            Arrays.sort(xx, Comparator.comparingInt(a -> a[1]));
            int m = 25;
            int sum = 0;
            for (int j = 26; j > 0; j--) {
                sum += j*xx[m--][1];
            }
            System.out.println(sum);
        }
    }
}
